package avicit.bdp.dms.dss.rest;

import avicit.bdp.common.service.dto.BdpColumnDTO;
import avicit.bdp.dms.dss.dto.CommonTableDto;
import avicit.bdp.dms.dss.dto.DataResourceDTO;
import avicit.bdp.dms.dss.dto.ResourceDTO;
import avicit.bdp.dms.dss.service.DataResourceService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据资源REST接口
 */
@RestController
@RequestMapping("/api/bdp/dms/dss/resource")
@Api(tags = "数据资源接口")
public class DataResourceRest {

    @Autowired
    private DataResourceService dataResourceService;

    /**
     *查询数据资源列表
     */
    @ApiOperation(value = "查询数据资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "项目空间ID", name = "projectId", dataType = "String"),
            @ApiImplicitParam(value = "资源名称", name = "name", dataType = "String"),
            @ApiImplicitParam(value = "状态", name = "status", dataType = "Int"),
            @ApiImplicitParam(value = "查询未订阅资源列表", name = "unSubscribeRes", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "currentPage", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/getDataResourceList")
    public ResponseMsg<QueryRespBean<DataResourceDTO>> getDataResourceList(@RequestParam(required = false) String typeId,
                                                                           @RequestParam(required = false) String projectId,
                                                                           @RequestParam(required = false) String name,
                                                                           @RequestParam(required = false, defaultValue = "0") String unSubscribeRes,
                                                                           @RequestParam(required = false) Integer status,
                                                                           @RequestParam(defaultValue = "1") Integer currentPage,
                                                                           @RequestParam(defaultValue = "10") Integer pageSize) throws Exception{
        ResponseMsg<QueryRespBean<DataResourceDTO>> responseMsg = new ResponseMsg<>();

        List<Integer> statusList = new ArrayList<>();
        if (status != null) {
            statusList.add(status);
        }

        QueryRespBean<DataResourceDTO> queryRespBean = dataResourceService.getDataResourceList(typeId, projectId, name,
                unSubscribeRes, statusList, currentPage, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    /**
     * 根据分类查询数据资源列表
     */
    @ApiOperation(value = "根据分类查询数据资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "currentPage", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @PostMapping("/getList")
    public ResponseMsg<QueryRespBean<DataResourceDTO>> getList(@RequestParam(required = false) String typeId,
                                                               @RequestParam(defaultValue = "1") Integer currentPage,
                                                               @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ResponseMsg<QueryRespBean<DataResourceDTO>> responseMsg = new ResponseMsg<>();

        List<Integer> statusList = new ArrayList<>();

        QueryRespBean<DataResourceDTO> queryRespBean = dataResourceService.getDataResourceList(typeId, null, null,
                "0", statusList, currentPage, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "根据条件查询数据资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数据资源实体类", name = "dataResourceDTO", dataType = "DataResourceDTO", paramType = "body"),
            @ApiImplicitParam(value = "当前页", name = "currentPage", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @PostMapping("/postList")
    public ResponseMsg<QueryRespBean<DataResourceDTO>> postList(@RequestParam(defaultValue = "1") Integer currentPage,
                                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                                @RequestBody DataResourceDTO dataResourceDTO) throws Exception {
        ResponseMsg<QueryRespBean<DataResourceDTO>> responseMsg = new ResponseMsg<>();

        List<Integer> statusList = new ArrayList<>();

        String typeId = dataResourceDTO.getTypeId();
        QueryRespBean<DataResourceDTO> queryRespBean = dataResourceService.getDataResourceList(typeId, null, null,
                "0", statusList, currentPage, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "新增数据资源")
    @PostMapping("/insertDataResource")
    public ResponseMsg<Boolean> insertDataResource(@RequestBody DataResourceDTO dataResourceDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        dataResourceService.insertDataResource(dataResourceDTO);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    @ApiOperation(value = "修改数据资源")
    @PostMapping("/updateDataResource")
    public ResponseMsg<Boolean> updateDataResource(@RequestBody DataResourceDTO dataResourceDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        dataResourceService.updateDataResource(dataResourceDTO);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }


    @ApiOperation(value = "删除数据资源")
    @DeleteMapping("/deleteDataResource")
    public ResponseMsg<Boolean> deleteDataResource(@RequestBody DataResourceDTO dataResourceDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataResourceService.deleteDataResource(dataResourceDTO.getId()));
        return responseMsg;
    }

    @ApiOperation(value = "查看数据资源详情")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @GetMapping("/getDataResourceById")
    public ResponseMsg<DataResourceDTO> getDataResourceById(@RequestParam String id) {
        ResponseMsg<DataResourceDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataResourceService.getDataResourceById(id));
        return responseMsg;
    }

    @ApiOperation(value = "发布/撤回")
    @PostMapping("/updateDataResourceStatus")
    public ResponseMsg<Boolean> updateDataResourceStatus(@RequestBody DataResourceDTO dataResourceDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        dataResourceService.updateDataResourceStatus(dataResourceDTO.getId(), dataResourceDTO.getStatus());
        return responseMsg;
    }

    @ApiOperation(value = "数据预览")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "String"),
            @ApiImplicitParam(value = "订阅ID", name = "subscribeId", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "currentPage", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/preview")
    public ResponseMsg<Map<String, Object>> preview(@RequestParam String id,
                                                    @RequestParam(required = false) String subscribeId,
                                                    @RequestParam(defaultValue = "1") Integer currentPage,
                                                    @RequestParam(defaultValue = "10") Integer pageSize) throws Exception{
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataResourceService.preview(id, subscribeId, currentPage, pageSize));
        return responseMsg;
    }

    @ApiOperation(value = "数据下载")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "String"),
            @ApiImplicitParam(value = "用户ID", name = "userId", dataType = "String"),
            @ApiImplicitParam(value = "下载类型", name = "type", dataType = "String")
    })
    @GetMapping("/download")
    public ResponseMsg<String> download(@RequestParam String id, @RequestParam String userId,
                                        @RequestParam(required = false) String type, HttpServletResponse response)
            throws Exception {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        dataResourceService.downloadDataFromDBToCsv(id, userId, type, response);
        return responseMsg;
    }

    /**
     * 获取模型分类及模型
     * 注意：
     *  1、数据开发工作台-数据发布，新增服务资源时获取项目模型（传入projectId）；
     *  2、数据服务，新增服务资源时获取公共模型（不传入projectId），并根据登录用户角色过滤模型分类及模型
     */
    @ApiOperation(value = "查询表管理树")
    @ApiImplicitParam(value = "projectId", name = "projectId", dataType = "String")
    @GetMapping("/getTableTree")
    public ResponseMsg<List<CommonTableDto>> getTableTree(@RequestParam(required = false) String projectId) throws Exception{
        ResponseMsg<List<CommonTableDto>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataResourceService.getTableTree(projectId));
        return responseMsg;
    }

    @ApiOperation(value = "根据表ID查询列")
    @ApiImplicitParam(value = "tableId", name = "tableId", dataType = "String")
    @GetMapping("/getColumnListByTableId")
    public ResponseMsg<List<BdpColumnDTO>> getColumnListByTableId(@RequestParam String tableId) throws Exception{
        ResponseMsg<List<BdpColumnDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataResourceService.getColumnListByTableId(tableId));
        return responseMsg;
    }

    @ApiOperation(value = "查询目录文件树")
    @ApiImplicitParam(value = "projectId", name = "projectId", dataType = "String")
    @GetMapping("/getResourceTree")
    public ResponseMsg<List<ResourceDTO>> getResourceTree(@RequestParam(required = false) String projectId) throws Exception{
        ResponseMsg<List<ResourceDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataResourceService.getResourceTree(projectId));
        return responseMsg;
    }

    /**
     * 查询服务目录列表
     */
    @ApiOperation(value = "查询服务目录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "服务名称", name = "name", dataType = "String"),
            @ApiImplicitParam(value = "预览量排序", name = "viewSort", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/getServiceCatalogList")
    public ResponseMsg<QueryRespBean<DataResourceDTO>> getServiceCatalogList(@RequestParam(required = false) String typeId,
                                                                             @RequestParam(required = false) String name,
                                                                             @RequestParam(required = false) String viewSort,
                                                                             @RequestParam(defaultValue = "1") Integer pageNo,
                                                                             @RequestParam(defaultValue = "10") Integer pageSize) throws Exception{
        ResponseMsg<QueryRespBean<DataResourceDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<DataResourceDTO> queryRespBean = dataResourceService.getServiceCatalogList(typeId, name, viewSort, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "更新查看次数")
    @PostMapping("/addViewCount")
    public ResponseMsg<String> addViewCount(@RequestBody DataResourceDTO dataResourceDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        dataResourceService.addViewCount(dataResourceDTO);
        return responseMsg;
    }

}
